//
//  FindNearViewController.m
//  HJZWeiBo
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "FindNearViewController.h"
#import "HJZStatus.h"
#import "HJZStatusFrame.h"
#import "HJZStatusTableViewCell.h"
#import "HJZStatusContentViewController.h"

@interface FindNearViewController ()<WBHttpRequestDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation FindNearViewController
- (void)request{
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    [WBHttpRequest requestWithURL:kFindNearUrl httpMethod:@"GET" params:@{@"access_token":token, @"lat":[NSString stringWithFormat:@"%f", kLocationX], @"long":[NSString stringWithFormat:@"%f", kLocationY]} delegate:self withTag:@"周边"];
}

- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *statusesArray = dic[@"statuses"];
    for (NSDictionary *dic in statusesArray) {
        HJZStatus *status = [[HJZStatus alloc] init];
        [status setValuesForKeysWithDictionary:dic];
        HJZStatusFrame *statusFrame = [[HJZStatusFrame alloc] init];
        statusFrame.status = status;
        [_dataSource addObject:statusFrame];
        [self.tableView reloadData];
    }
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    self.dataSource = [NSMutableArray array];
    [self request];
    
    
    
    // Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJZStatusTableViewCell *cell = [HJZStatusTableViewCell cellWithTableView:tableView];
    
    cell.statusFrame = self.dataSource[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.dataSource[indexPath.row] cellHeight];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJZStatusContentViewController *statusContentVC = [[HJZStatusContentViewController alloc] init];
    statusContentVC.statusFrame = _dataSource[indexPath.row];
    [self.navigationController pushViewController:statusContentVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
